<?php

namespace App\Http\Controllers\Admin;

use App\Models\ResourceReadRecom;
use App\Validate\ResourceReadRecomValidate;
use Illuminate\Support\Facades\DB;

/**
 * 数字资源推荐阅读表（书单形式推荐）
 * Class Banner
 * @package app\api\controller
 */
class ResourceReadRecomController extends CommonController
{
    public $model;
    public $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->model = new ResourceReadRecom();
        $this->validate = new ResourceReadRecomValidate();
    }


    /**
     * 筛选(用于下拉框选择)
     */
    public function filterList()
    {
        $condition[] = ['is_del', '=', 1];

        return $this->model->getFilterList(['id', 'name', 'intro'], $condition);
    }


    /**
     * 列表
     * @param page int 页码
     * @param limit int 分页大小
     * @param is_play int 发布   1 发布  2 撤销
     * @param keywords string 搜索关键词
     */
    public function lists()
    {
        $page = $this->request->page ? intval($this->request->page) : 1;
        $limit = $this->request->limit ? $this->request->limit : 10;
        $res = $this->model->lists($this->request->all());
        if (empty($res['data'])) {
            return $this->returnApi(203, "暂无数据");
        }
        $res = $this->disPageData($res);
        $res['data'] = $this->addSerialNumber($res['data'], $page, $limit);

        return $this->returnApi(200, "查询成功", true, $res);
    }


    /**
     * 详情
     * @param id int
     */
    public function detail()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('detail')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $res = $this->model->detail($this->request->id);

        if (empty($res)) {
            return $this->returnApi(203, "暂无数据");
        }

        return $this->returnApi(200, "查询成功", true, $res);
    }

    /**
     * 新增
     * @param img string 书单封面
     * @param name string 书单名称
     * @param intro string 简介
     * @param recommendation string 荐书语
     */
    public function add()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('add')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        $is_exists = $this->model->nameIsExists($this->request->name, 'name');

        if ($is_exists) {
            return $this->returnApi(202, "此书单名称已存在");
        }
        $this->request->merge(['manage_id' => $this->request->manage_id]);
        $res = $this->model->add($this->request->all());

        if (!$res) {
            return $this->returnApi(202, "新增失败");
        }
        return $this->returnApi(200, "新增成功", true);
    }


    /**
     * 修改
     * @param id int banner id
     * @param img string 书单封面
     * @param name string 书单名称
     * @param intro string 简介
     * @param recommendation string 荐书语
     */
    public function change()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('change')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        $is_exists = $this->model->nameIsExists($this->request->name, 'name', $this->request->id);
        if ($is_exists) {
            return $this->returnApi(202, "此书单名称已存在");
        }
        $res = $this->model->change($this->request->all());

        if (!$res) {
            return $this->returnApi(202, "修改失败");
        }
        return $this->returnApi(200, "修改成功", true);
    }

    /**
     * 删除
     * @param id int
     */
    public function del()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('del')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $res = $this->model->del($this->request->id);
        if ($res === true) {
            return $this->returnApi(200, "删除成功", true);
        }
        return $this->returnApi(202, $res);
    }

    /**
     * 撤销 和 发布
     * @param ids int 书单id  多个逗号拼接  all全部
     * @param is_play int 发布   1 发布  2 撤销
     */
    public function cancelAndRelease()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('cancel_and_release')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        DB::beginTransaction();
        try {
            list($code, $msg) = $this->model->resumeAndCancel($this->request->ids, 'is_play', $this->request->is_play);

            $is_play = $this->request->is_play == 1 ? '发布' : '撤销';

            DB::commit();
            return $this->returnApi($code, $is_play . $msg, $code === 200 ? true : false);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, $e->getMessage());
        }
    }
}
